TARGET=scsolver.uno
PACKAGE=$(TARGET).oxt

SCSOLVERDIR=@SCSOLVERDIR@
SRCDIR=$(SCSOLVERDIR)/source
OBJDIR=@OBJDIR@
BINDIR=@BINDIR@
PLATFORM=@PLATFORM@

LDFLAGS= -lsal -lcppuhelpergcc3 -lcppu -lstlport_gcc -L@OOO_INSTALL_PATH@/program
CXXCPP=@UNO_INCLUDE@ -I$(SRCDIR)/inc -I. -I$(OBJDIR)
CXX_DEFINES= \
	-DUNX -DGCC -DLINUX -DCPPU_ENV=gcc3 -DHAVE_GCC_VISIBILITY_FEATURE

CXX_SHARED_FLAGS=-fPIC -fno-common -export-dynamic -fvisibility=hidden
LIBLPSOLVE=@LIBLPSOLVE_ARCHIVE_PATH@
CPPFLAGS=-DDEBUG -Wall -Wno-non-virtual-dtor -Wno-strict-aliasing -g -O0

SRVDIR=$(SRCDIR)/service
UIDIR=$(SRCDIR)/ui
TOOLDIR=$(SRCDIR)/tool

HEADER= \
	$(SRCDIR)/inc/tool/timer.hxx \
	$(SRCDIR)/inc/tool/global.hxx \
	$(SRCDIR)/inc/optiondlg.hxx \
	$(SRCDIR)/inc/basedlg.hxx \
	$(SRCDIR)/inc/type.hxx \
	$(SRCDIR)/inc/unoglobal.hxx \
	$(SRCDIR)/inc/xcalc.hxx \
	$(SRCDIR)/inc/option.hxx \
	$(SRCDIR)/inc/baselistener.hxx \
	$(SRCDIR)/inc/dialog.hxx \
	$(SRCDIR)/inc/lpbuilder.hxx \
	$(SRCDIR)/inc/solver.hxx \
	$(SRCDIR)/inc/resmgr.hxx \
	$(SRCDIR)/inc/listener.hxx \
	$(SRCDIR)/inc/numeric/diff.hxx \
	$(SRCDIR)/inc/numeric/type.hxx \
	$(SRCDIR)/inc/numeric/exception.hxx \
	$(SRCDIR)/inc/numeric/quasinewton.hxx \
	$(SRCDIR)/inc/numeric/matrix.hxx \
	$(SRCDIR)/inc/numeric/funcobj.hxx \
	$(SRCDIR)/inc/numeric/lpsolve.hxx \
	$(SRCDIR)/inc/numeric/lpbase.hxx \
	$(SRCDIR)/inc/numeric/lpmodel.hxx \
	$(SRCDIR)/inc/numeric/cellfuncobj.hxx \
	$(SRCDIR)/inc/numeric/nlpmodel.hxx \
	$(SRCDIR)/inc/numeric/polyeqnsolver.hxx \
	$(SRCDIR)/inc/numeric/nlpbase.hxx \
	$(SRCDIR)/inc/numeric/cycliccoordinate.hxx \
	$(SRCDIR)/inc/numeric/hookejeeves.hxx \
	$(SRCDIR)/inc/numeric/rosenbrock.hxx \
	$(SRCDIR)/inc/numeric/quadfitlinesearch.hxx \
	$(SRCDIR)/inc/unohelper.hxx \
	$(SRCDIR)/inc/msgdlg.hxx \
	$(SRCDIR)/inc/solvemodel.hxx \
	$(SRCDIR)/inc/nlpbuilder.hxx

OBJFILES= \
	$(OBJDIR)/optiondlg.o \
	$(OBJDIR)/basedlg.o \
	$(OBJDIR)/unoglobal.o \
	$(OBJDIR)/xcalc.o \
	$(OBJDIR)/option.o \
	$(OBJDIR)/baselistener.o \
	$(OBJDIR)/dialog.o \
	$(OBJDIR)/lpbuilder.o \
	$(OBJDIR)/listener.o \
	$(OBJDIR)/unohelper.o \
	$(OBJDIR)/msgdlg.o \
	$(OBJDIR)/resmgr.o \
	$(OBJDIR)/solvemodel.o \
	$(OBJDIR)/nlpbuilder.o \
	$(OBJDIR)/timer.o \
	$(OBJDIR)/global.o \
	$(OBJDIR)/service.o

NUMERIC_OBJFILES = \
	$(OBJDIR)/baselinesearch.o \
	$(OBJDIR)/cellfuncobj.o \
	$(OBJDIR)/cycliccoordinate.o \
	$(OBJDIR)/diff.o \
	$(OBJDIR)/exception.o \
	$(OBJDIR)/funcobj.o \
	$(OBJDIR)/hookejeeves.o \
	$(OBJDIR)/lpbase.o \
	$(OBJDIR)/lpmodel.o \
	$(OBJDIR)/lpsolve.o \
	$(OBJDIR)/matrix.o \
	$(OBJDIR)/nlpbase.o \
	$(OBJDIR)/nlpmodel.o \
	$(OBJDIR)/polyeqnsolver.o \
	$(OBJDIR)/quadfitlinesearch.o \
	$(OBJDIR)/quasinewton.o \
	$(OBJDIR)/rosenbrock.o

XCUFILES = Addons.xcu ProtocolHandler.xcu

MISCFILES = \
	description.xml \
	license.txt

all: pre @GEN_UNOHEADERS_TARGET@ $(PACKAGE)

pre:
	mkdir $(OBJDIR) $(PLATFORM) 2> /dev/null || /bin/true && \
	ln -s @LIBLPSOLVE_PATH@ $(OBJDIR)/lpsolve 2> /dev/null || /bin/true

#----------------------------------------------------------------------------
# numeric

NUMDIR=$(SRCDIR)/numeric

$(OBJDIR)/numeric.a: pre $(NUMERIC_OBJFILES)
	$(AR) r $@ $(NUMERIC_OBJFILES)

$(OBJDIR)/baselinesearch.o: $(HEADER) $(NUMDIR)/baselinesearch.cxx
	$(CXX) $(CPPFLAGS) $(CXXCPP) $(CXX_DEFINES) $(CPP_LIBS) \
	-o $@ $(CXX_SHARED_FLAGS) -c $(NUMDIR)/baselinesearch.cxx

$(OBJDIR)/funcobj.o: $(HEADER) $(NUMDIR)/funcobj.cxx
	$(CXX) $(CPPFLAGS) $(CXXCPP) $(CXX_DEFINES) $(CPP_LIBS) \
	-o $@ $(CXX_SHARED_FLAGS) -c $(NUMDIR)/funcobj.cxx

$(OBJDIR)/matrix.o: $(HEADER) $(NUMDIR)/matrix.cxx
	$(CXX) $(CPPFLAGS) $(CXXCPP) $(CXX_DEFINES) $(CPP_LIBS) \
	-o $@ $(CXX_SHARED_FLAGS) -c $(NUMDIR)/matrix.cxx

$(OBJDIR)/diff.o: $(HEADER) $(NUMDIR)/diff.cxx
	$(CXX) $(CPPFLAGS) $(CXXCPP) $(CXX_DEFINES) $(CPP_LIBS) \
	-o $@ $(CXX_SHARED_FLAGS) -c $(NUMDIR)/diff.cxx

$(OBJDIR)/lpbase.o: $(HEADER) $(NUMDIR)/lpbase.cxx
	$(CXX) $(CPPFLAGS) $(CXXCPP) $(CXX_DEFINES) $(CPP_LIBS) \
	-o $@ $(CXX_SHARED_FLAGS) -c $(NUMDIR)/lpbase.cxx

$(OBJDIR)/lpmodel.o: $(HEADER) $(NUMDIR)/lpmodel.cxx
	$(CXX) $(CPPFLAGS) $(CXXCPP) $(CXX_DEFINES) $(CPP_LIBS) \
	-o $@ $(CXX_SHARED_FLAGS) -c $(NUMDIR)/lpmodel.cxx

$(OBJDIR)/cellfuncobj.o: $(HEADER) $(NUMDIR)/cellfuncobj.cxx
	$(CXX) $(CPPFLAGS) $(CXXCPP) $(CXX_DEFINES) $(CPP_LIBS) \
	-o $@ $(CXX_SHARED_FLAGS) -c $(NUMDIR)/cellfuncobj.cxx

$(OBJDIR)/lpsimplex.o: $(HEADER) $(NUMDIR)/lpsimplex.cxx
	$(CXX) $(CPPFLAGS) $(CXXCPP) $(CXX_DEFINES) $(CPP_LIBS) \
	-o $@ $(CXX_SHARED_FLAGS) -c $(NUMDIR)/lpsimplex.cxx

$(OBJDIR)/lpsolve.o: $(HEADER) $(NUMDIR)/lpsolve.cxx
	$(CXX) $(CPPFLAGS) $(CXXCPP) $(CXX_DEFINES) $(CPP_LIBS) \
	-o $@ $(CXX_SHARED_FLAGS) -c $(NUMDIR)/lpsolve.cxx

$(OBJDIR)/nlpmodel.o: $(HEADER) $(NUMDIR)/nlpmodel.cxx
	$(CXX) $(CPPFLAGS) $(CXXCPP) $(CXX_DEFINES) $(CPP_LIBS) \
	-o $@ $(CXX_SHARED_FLAGS) -c $(NUMDIR)/nlpmodel.cxx

$(OBJDIR)/polyeqnsolver.o: $(HEADER) $(NUMDIR)/polyeqnsolver.cxx
	$(CXX) $(CPPFLAGS) $(CXXCPP) $(CXX_DEFINES) $(CPP_LIBS) \
	-o $@ $(CXX_SHARED_FLAGS) -c $(NUMDIR)/polyeqnsolver.cxx

$(OBJDIR)/nlpbase.o: $(HEADER) $(NUMDIR)/nlpbase.cxx
	$(CXX) $(CPPFLAGS) $(CXXCPP) $(CXX_DEFINES) $(CPP_LIBS) \
	-o $@ $(CXX_SHARED_FLAGS) -c $(NUMDIR)/nlpbase.cxx

$(OBJDIR)/quasinewton.o: $(HEADER) $(NUMDIR)/quasinewton.cxx
	$(CXX) $(CPPFLAGS) $(CXXCPP) $(CXX_DEFINES) $(CPP_LIBS) \
	-o $@ $(CXX_SHARED_FLAGS) -c $(NUMDIR)/quasinewton.cxx

$(OBJDIR)/cycliccoordinate.o: $(HEADER) $(NUMDIR)/cycliccoordinate.cxx
	$(CXX) $(CPPFLAGS) $(CXXCPP) $(CXX_DEFINES) $(CPP_LIBS) \
	-o $@ $(CXX_SHARED_FLAGS) -c $(NUMDIR)/cycliccoordinate.cxx

$(OBJDIR)/hookejeeves.o: $(HEADER) $(NUMDIR)/hookejeeves.cxx
	$(CXX) $(CPPFLAGS) $(CXXCPP) $(CXX_DEFINES) $(CPP_LIBS) \
	-o $@ $(CXX_SHARED_FLAGS) -c $(NUMDIR)/hookejeeves.cxx

$(OBJDIR)/rosenbrock.o: $(HEADER) $(NUMDIR)/rosenbrock.cxx
	$(CXX) $(CPPFLAGS) $(CXXCPP) $(CXX_DEFINES) $(CPP_LIBS) \
	-o $@ $(CXX_SHARED_FLAGS) -c $(NUMDIR)/rosenbrock.cxx

$(OBJDIR)/quadfitlinesearch.o: $(HEADER) $(NUMDIR)/quadfitlinesearch.cxx
	$(CXX) $(CPPFLAGS) $(CXXCPP) $(CXX_DEFINES) $(CPP_LIBS) \
	-o $@ $(CXX_SHARED_FLAGS) -c $(NUMDIR)/quadfitlinesearch.cxx

$(OBJDIR)/exception.o: $(HEADER) $(NUMDIR)/exception.cxx
	$(CXX) $(CPPFLAGS) $(CXXCPP) $(CXX_DEFINES) $(CPP_LIBS) \
	-o $@ $(CXX_SHARED_FLAGS) -c $(NUMDIR)/exception.cxx

$(OBJDIR)/scsolver.a: pre $(OBJFILES)
	$(AR) r $@ $(OBJFILES)

#----------------------------------------------------------------------------
# scsolver

$(OBJDIR)/service.o: $(HEADER) $(SRVDIR)/service.cxx
	$(CXX) $(CPPFLAGS) $(CXXCPP) $(CXX_DEFINES) $(CPP_LIBS) \
	-o $@ $(CXX_SHARED_FLAGS) -c $(SRVDIR)/service.cxx

$(OBJDIR)/basedlg.o: $(HEADER) $(UIDIR)/basedlg.cxx
	$(CXX) $(CPPFLAGS) $(CXXCPP) $(CXX_DEFINES) $(CPP_LIBS) \
	-o $@ $(CXX_SHARED_FLAGS) -c $(UIDIR)/basedlg.cxx

$(OBJDIR)/baselistener.o: $(HEADER) $(UIDIR)/baselistener.cxx
	$(CXX) $(CPPFLAGS) $(CXXCPP) $(CXX_DEFINES) $(CPP_LIBS) \
	-o $@ $(CXX_SHARED_FLAGS) -c $(UIDIR)/baselistener.cxx

$(OBJDIR)/dialog.o: $(HEADER) $(UIDIR)/dialog.cxx
	$(CXX) $(CPPFLAGS) $(CXXCPP) $(CXX_DEFINES) $(CPP_LIBS) \
	-o $@ $(CXX_SHARED_FLAGS) -c $(UIDIR)/dialog.cxx

$(OBJDIR)/optiondlg.o: $(HEADER) $(UIDIR)/optiondlg.cxx
	$(CXX) $(CPPFLAGS) $(CXXCPP) $(CXX_DEFINES) $(CPP_LIBS) \
	-o $@ $(CXX_SHARED_FLAGS) -c $(UIDIR)/optiondlg.cxx

$(OBJDIR)/option.o: $(HEADER) $(UIDIR)/option.cxx
	$(CXX) $(CPPFLAGS) $(CXXCPP) $(CXX_DEFINES) $(CPP_LIBS) \
	-o $@ $(CXX_SHARED_FLAGS) -c $(UIDIR)/option.cxx

$(OBJDIR)/global.o: $(HEADER) $(TOOLDIR)/global.cxx
	$(CXX) $(CPPFLAGS) $(CXXCPP) $(CXX_DEFINES) $(CPP_LIBS) \
	-o $@ $(CXX_SHARED_FLAGS) -c $(TOOLDIR)/global.cxx

$(OBJDIR)/listener.o: $(HEADER) $(UIDIR)/listener.cxx
	$(CXX) $(CPPFLAGS) $(CXXCPP) $(CXX_DEFINES) $(CPP_LIBS) \
	-o $@ $(CXX_SHARED_FLAGS) -c $(UIDIR)/listener.cxx

$(OBJDIR)/lpbuilder.o: $(HEADER) $(UIDIR)/lpbuilder.cxx
	$(CXX) $(CPPFLAGS) $(CXXCPP) $(CXX_DEFINES) $(CPP_LIBS) \
	-o $@ $(CXX_SHARED_FLAGS) -c $(UIDIR)/lpbuilder.cxx

$(OBJDIR)/nlpbuilder.o: $(HEADER) $(UIDIR)/nlpbuilder.cxx
	$(CXX) $(CPPFLAGS) $(CXXCPP) $(CXX_DEFINES) $(CPP_LIBS) \
	-o $@ $(CXX_SHARED_FLAGS) -c $(UIDIR)/nlpbuilder.cxx

$(OBJDIR)/msgdlg.o: $(HEADER) $(UIDIR)/msgdlg.cxx
	$(CXX) $(CPPFLAGS) $(CXXCPP) $(CXX_DEFINES) $(CPP_LIBS) \
	-o $@ $(CXX_SHARED_FLAGS) -c $(UIDIR)/msgdlg.cxx

$(OBJDIR)/resmgr.o: $(HEADER) $(UIDIR)/resmgr.cxx
	$(CXX) $(CPPFLAGS) $(CXXCPP) $(CXX_DEFINES) $(CPP_LIBS) \
	-o $@ $(CXX_SHARED_FLAGS) -c $(UIDIR)/resmgr.cxx

$(OBJDIR)/solvemodel.o: $(HEADER) $(UIDIR)/solvemodel.cxx
	$(CXX) $(CPPFLAGS) $(CXXCPP) $(CXX_DEFINES) $(CPP_LIBS) \
	-o $@ $(CXX_SHARED_FLAGS) -c $(UIDIR)/solvemodel.cxx

$(OBJDIR)/unoglobal.o: $(HEADER) $(UIDIR)/unoglobal.cxx
	$(CXX) $(CPPFLAGS) $(CXXCPP) $(CXX_DEFINES) $(CPP_LIBS) \
	-o $@ $(CXX_SHARED_FLAGS) -c $(UIDIR)/unoglobal.cxx

$(OBJDIR)/unohelper.o: $(HEADER) $(UIDIR)/unohelper.cxx
	$(CXX) $(CPPFLAGS) $(CXXCPP) $(CXX_DEFINES) $(CPP_LIBS) \
	-o $@ $(CXX_SHARED_FLAGS) -c $(UIDIR)/unohelper.cxx

$(OBJDIR)/xcalc.o: $(HEADER) $(UIDIR)/xcalc.cxx
	$(CXX) $(CPPFLAGS) $(CXXCPP) $(CXX_DEFINES) $(CPP_LIBS) \
	-o $@ $(CXX_SHARED_FLAGS) -c $(UIDIR)/xcalc.cxx

$(OBJDIR)/timer.o: $(HEADER) $(TOOLDIR)/timer.cxx
	$(CXX) $(CPPFLAGS) $(CXXCPP) $(CXX_DEFINES) $(CPP_LIBS) \
	-o $@ $(CXX_SHARED_FLAGS) -c $(TOOLDIR)/timer.cxx

#----------------------------------------------------------------------------
# misc

$(OBJDIR)/unoheaders:
	export LD_LIBRARY_PATH=@RUNTIME_LIB_PATH@ && \
	$(BINDIR)/cppumaker -BUCR @OOO_INSTALL_PATH@/program/types.rdb \
		-O$(OBJDIR) && \
	touch $@

$(PACKAGE): pre $(PLATFORM)/$(TARGET).so
	../bin/make_manifest.py META-INF/manifest.xml $(PLATFORM)/$(TARGET).so $(PLATFORM) && \
	zip -j $@ $(OBJDIR)/$(TARGET).rdb $(XCUFILES) && \
	zip -r $@ $(MISCFILES) && \
	find $(PLATFORM) translation help META-INF -type f | grep -v ".svn" | xargs zip -r $@

$(PLATFORM)/$(TARGET).so: $(NUMERIC_OBJFILES) $(OBJFILES)
	export LD_LIBRARY_PATH=@RUNTIME_LIB_PATH@ && \
	$(CXX) -shared -Wl,-soname,$(TARGET).so -o $@ -lc \
		$(NUMERIC_OBJFILES) $(OBJFILES) $(LIBLPSOLVE) && \
	$(BINDIR)/regcomp -register -r $(OBJDIR)/$(TARGET).rdb -c $@

regview:
	export LD_LIBRARY_PATH=@RUNTIME_LIB_PATH@ && \
	$(BINDIR)/regview $(OBJDIR)/$(TARGET).rdb

install:
	@OOO_INSTALL_PATH@/program/unopkg add -f $(PACKAGE)

clean:
	rm -rf $(OBJFILES) $(NUMERIC_OBJFILES) $(PLATFORM)

distclean: clean
	rm -rf $(OBJDIR) $(PACKAGE)

